package com.zcgame.rxjavademo

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.ObservableSource
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Function
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //initRxJust()
        //initRxTimer()
        //initRxInterval()
        initFlatMap()
    }

    @SuppressLint("CheckResult")
    /**
     * 定时操作
     */
    private fun initRxTimer() {
        //用于管理观察者生命周期，可以持有多个Disposable
        //如果只有一个Disposable 可直接调用di
        val mCompositeDisposable = CompositeDisposable()
        mCompositeDisposable.add(
                Observable.timer(1000,TimeUnit.MILLISECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeWith(object : DisposableObserver<Long>() {

                            override fun onComplete() {
                                mCompositeDisposable.clear()
                            }

                            override fun onNext(t: Long) {
                                Log.d("huanglj","onNext-->  $t")
                            }

                            override fun onError(e: Throwable) {
                                mCompositeDisposable.clear()
                            }
                        })
        )
    }

    /**
     * 轮询操作
     */
    private fun initRxInterval(){
        val mCompositeDisposable = CompositeDisposable()
        Observable.interval(0,1000L,TimeUnit.MILLISECONDS)
                .observeOn(Schedulers.io())
                .subscribe(object :Observer<Long>{
                    override fun onComplete() {
                        Log.d("huanglj","onComplete")
                        mCompositeDisposable.clear()
                    }

                    override fun onSubscribe(d: Disposable) {
                        mCompositeDisposable.add(d)
                    }

                    override fun onNext(t: Long) {
                        Log.d("huanglj","onNext-->  $t")
                        if(t == 10L){
                            onComplete()
                        }
                    }

                    override fun onError(e: Throwable) {
                        mCompositeDisposable.clear()
                    }
                })
    }

    /**
     * Just操作符的简单使用
     * D/huanglj: onSubscribe--> null
     * D/huanglj: onNext-->  1
     * D/huanglj: onNext-->  2
     * D/huanglj: onNext-->  3
     * D/huanglj: onNext-->  4
     * D/huanglj: onNext-->  5
     * D/huanglj: onNext-->  6
     * D/huanglj: onNext-->  7
     * D/huanglj: onNext-->  8
     * D/huanglj: onNext-->  9
     * D/huanglj: onNext-->  10
     * D/huanglj: onComplete
     */
    private fun initRxJust() {
        Observable.just(1,2,3,4,5,6,7,8,9,10)
                .subscribeOn(Schedulers.io()).subscribe(object :Observer<Int>{
                    override fun onComplete() {
                        Log.d("huanglj","onComplete")
                    }

                    override fun onSubscribe(d: Disposable) {
                        Log.d("huanglj","onSubscribe--> $d")
                    }

                    override fun onNext(t: Int) {
                        Log.d("huanglj","onNext-->  $t")
                    }

                    override fun onError(e: Throwable) {
                        Log.d("huanglj","$e")
                    }
                })
    }

    /**
     * 返回Observable
     */
    @SuppressLint("CheckResult")
    private fun initFlatMap() {
//        Observable.create(ObservableOnSubscribe<Int> {
//            it.onNext(1); it.onNext(2);it.onNext(3)
//        }).observeOn(Schedulers.newThread()).subscribeOn(Schedulers.io())
//                .flatMap(Function<Int,ObservableSource<String>>{i ->
//                   Log.d("huanglj","i的值--> $i")
//                    ObservableSource {
//                        it.onNext("haha")
//                        it.onComplete()
//                    }
//                }).subscribe {
//                    Log.d("huanglj",it)
//                }


//        Observable.just(1,2,3,4).observeOn(Schedulers.newThread()).subscribeOn(Schedulers.io())
//                .flatMap(Function<Int,ObservableSource<String>>{i ->
//                    Log.d("huanglj","i的值--> $i")
//                    ObservableSource {
//                        it.onNext("haha + $i")
//                        it.onComplete()
//                    }
//                }).subscribe {
//                    Log.d("huanglj",it)
//                }
        //一对一
        Observable.just(getData()).flatMap { list ->
            Observable.just(list)
        }.subscribe {
            Log.d("huanglj",it.toString())
        }

        //[Student(name=null, number=0), Student(name=null, number=1), Student(name=null, number=2), Student(name=null, number=3), Student(name=null, number=4), Student(name=null, number=5)]

        //一对多
        Observable.just(getData()).flatMap { list ->
            Observable.fromIterable(list)
        }.subscribe {
            Log.d("huanglj",it.toString())
        }
        //Student(name=null, number=0)
        //Student(name=null, number=1)
        //Student(name=null, number=2)
        //Student(name=null, number=3)
        //Student(name=null, number=4)
        //Student(name=null, number=5)


    }

    private fun initMap(){
    }

    private fun getData():MutableList<Student>{
        val data = mutableListOf<Student>()
        for(i in 0..5){
            data.add(Student(number = i))
        }
        return data
    }

}